<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('编辑')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'400px':'450px'" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'90px':'155px'" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item :label="$t('聚合名字')" prop="name">
          <div>{{record.name}}</div>
        </el-form-item>
        <el-form-item :label="$t('聚合模式')" prop="mode">
          <el-select v-model="record.mode" size="small" ref="mode">
            <el-option v-for="item in modeList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('分包方式')" prop="distribute">
          <el-select v-model="record.distribute" size="small" ref="distribute">
            <el-option v-for="item in distributeList" :key="item.value" :label="item.lable" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { editBondInfo, getBondInfo } from '@/api/network/api/bond'
import { selectClass } from '@/utils/input-class.js'
export default {
  name: 'BondIpDialog',
  data () {
    var validateMode = (rule, value, callback) => {
      if (value && value.length > 0) {
        callback()
      } else {
        callback(new Error(this.$t('不能为空')))
      }
    }
    return {
      dialogVisible: false,
      lang: this.$i18n.locale,
      record: {
        name: '',
        mode: '',
        distribute: ''
      },
      recordCopy: {
        name: '',
        mode: '',
        distribute: ''
      },
      modeList: ['static', 'dynamic'],
      distributeList: [
        { lable: this.$t('源MAC+目的MAC'), value: 'SD-MAC' },
        { lable: this.$t('源目的端口+源目的IP'), value: 'SDPORT-SDIP' },
        { lable: this.$t('源目的MAC+源目的IP'), value: 'SDMAC-SDIP' },
        { lable: this.$t('T-源目的端口+源目的IP'), value: 'T-SDPORT-SDIP' },
        { lable: this.$t('T-源目的MAC+源目的IP'), value: 'T-SDMAC-SDIP' }
      ],
      rules: {
        mode: [{ required: false, validator: validateMode, trigger: 'change' }],
        distribute: [{ required: false, validator: validateMode, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const selectData = ['mode', 'distribute']
        selectClass(this.$refs, selectData)
      })
      if (data) {
        getBondInfo({ name: data.BondName }).then(res => {
          if (res.status === 0) {
            this.record.name = res.info[0].BondName
            this.record.mode = res.info[0].mode
            this.record.distribute = res.info[0].distribute
            for (let item of this.distributeList) {
              if (res.info[0].distribute === item.lable) {
                this.record.distribute = item.value
                break
              }
            }
          }
        })
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          editBondInfo(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('编辑成功'),
                type: 'success'
              })
              this.close()
              this.$emit('listData')
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}
</style>
